#pragma once
//文件版本
#include <iostream>
#include <unordered_map>
#include <vector>
#include <string>
#include <fstream>
#include "../comm/Log.hpp"
#include "../comm/util.hpp"
#include <cstdlib>
#include <cassert>

namespace oj_model
{
    using namespace std;
    using namespace ns_log;
    using namespace ns_util;

    // 1 回文数 简单 ./oj_questions/1 1 5000
    struct Question
    {
        string _num;
        string _title;
        string _star;
        int _cpu_rlimit;
        int _mem_rlimit;
        string _desc;
        string _header;
        string _tail;
    };

    static const string questionlist = "./questions/questions.list";
    static const string question = "./questions/";
    class Model
    {
    public:
        Model()
        {
            assert(LoadQuestionList(questionlist));
        }
        ~Model()
        {
        }

        // 根据题目list文件，加载所有的题目信息到内存中
        //  model: 主要用来和数据进行交互，对外提供访问数据的接口
        bool LoadQuestionList(const string question_list)
        {
            ifstream in(question_list);
            if (!in.is_open())
            {
                LOG(ERROR, "打开题库文件失败,请查看是否存在");
                return false;
            }

            LOG(INFO, "打开题库文件成功");
            string line;
            while (getline(in, line))
            {
                vector<string> out;
                StringUtil::SplitString(line, &out, " ");
                if (out.size() != 5)
                {
                    LOG(WARNNING, "部分题目无法填入maps,请检查是否为格式问题");
                    return false;
                }
                Question q;
                q._num = out[0];
                q._title = out[1];
                q._star = out[2];
                q._cpu_rlimit = atoi(out[3].c_str());
                q._mem_rlimit = atoi(out[4].c_str());

                string path = question;
                path += q._num;
                path += "/";

                FileUtil::ReadFile(path + "desc.txt", &q._desc, true);
                FileUtil::ReadFile(path + "Header.cc", &q._header, true);
                FileUtil::ReadFile(path + "tail.cc", &q._tail, true);

                // 把每个 q 加载到maps里
                _questionsmap[q._num] = q;
            }

            LOG(INFO, "加载题库成功.....!");

            in.close();
            return true;
        }

        bool GetAllQuestions(vector<Question> *out)
        {
            if (_questionsmap.size() == 0)
            {
                LOG(ERROR, "用户获取题库失败，题库为空");
                return false;
            }
            for (auto &ques : _questionsmap)
            {
                out->push_back(ques.second);
            }
            return true;
        }

        bool GetOneQuestion(const string &num, Question *out)
        {
            const auto& iter = _questionsmap.find(num);
            if (iter == _questionsmap.end())
            {
                LOG(ERROR, "用户获取题目失败");
                return false;
            }
            *out = iter->second;
            return true;
        }

    private:
        unordered_map<string, Question> _questionsmap;
    };
};